package cn.com.headfree.evaluation.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.evaluation.service.IDeclareEvaluationService;
import cn.com.headfree.util.ExcelExportUtil;
import com.gta.edu.sdk.common.dto.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 专家人才评价
 * @Author wcSun
 * @Date 2022/6/27
 **/
@Controller
@RequestMapping(value = "/declare/evaluation", produces = "application/json;charset=UTF-8")
public class DeclareEvaluationController extends ExBaseController {

    private static final Logger LOG = LogManager.getLogger(DeclareEvaluationController.class);

    @Autowired
    private IDeclareEvaluationService declareEvaluationService;

    /**
     * 保存
     */
    @RequestMapping("/saveDeclareEvaluationInfo")
    @ResponseBody
    public ResultData saveDeclareEvaluationInfo(HttpServletRequest request, TDeclareEvaluationInfo declareEvaluationInfo) {
        ResultData rs = new ResultData();
        User user = this.getLoginUserInfo(request);
        try {
            declareEvaluationInfo.setErpNo(user.getFax());
            declareEvaluationInfo.setUserName(user.getName());
            List<TDeclareEvaluationInfo> declareEvaluationInfos = declareEvaluationService.judge(declareEvaluationInfo);
            if (CollectionUtils.isNotEmpty(declareEvaluationInfos)) {
                rs.setSuccess(false);
                rs.setErrorMsg("当前年度已申请调整");
            } else {
                declareEvaluationInfo.setErpNo(user.getFax());
                declareEvaluationInfo.setUserName(user.getName());
                declareEvaluationInfo.setOrgId(user.getBaseOrgId());
                declareEvaluationInfo.setOrgName(user.getBaseOrgName());
                declareEvaluationService.saveDeclareEvaluationInfo(declareEvaluationInfo);
                rs.setSuccess(true);
                LOG.info("保存成功");
            }
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("保存失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 分页查询列表
     *
     * @param page
     * @param year
     * @return
     */
    @RequestMapping("/getPageDeclareEvaluationInfo")
    @ResponseBody
    public ResultData getPageDeclareEvaluationInfo(Page<TDeclareEvaluationInfo> page, String year, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            User user = this.getLoginUserInfo(request);
            declareEvaluationService.getPageDeclareEvaluationInfo(page, year, user);
            rs.setData(page);
            LOG.info("查询成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查询失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 管理员审核同意：1、申报状态设为同意
     */
    @RequestMapping("/auditAgreeEvaluationDeclareInfo")
    @ResponseBody
    public ResultData auditAgreeEvaluationDeclareInfo(HttpServletRequest request,String declareId){
        ResultData result = new ResultData();
        try{
            User user = this.getLoginUserInfo(request);
            declareEvaluationService.auditAgreeInfo(declareId,user);
            result.setSuccess(true);
            result.setData("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorMsg("操作失败");
        }
        return result;
    }

    /**
     * 管理员驳回：1、删除积分明细；2、申报状态设为不同意
     */
    @RequestMapping("/auditRejectEvaluationDeclareInfo")
    @ResponseBody
    public ResultData auditRejectEvaluationDeclarePointInfo(HttpServletRequest request,String declareId,String auditOpt){
        ResultData result = new ResultData();
        try{
            User user = this.getLoginUserInfo(request);
            declareEvaluationService.auditRejectInfo(declareId,user,auditOpt);
            result.setSuccess(true);
            result.setData("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorMsg("操作失败");
        }
        return result;
    }

    /**
     * 分页查询列表:人才申报评价查询
     *
     * @param page
     * @return
     */
    @RequestMapping("/getPageDeclareEvaluationQuery")
    @ResponseBody
    public ResultData getPageDeclareEvaluationQuery(Page<TDeclareEvaluationInfo> page, TDeclareEvaluationInfo declareEvaluationInfo, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            User user = this.getLoginUserInfo(request);
            declareEvaluationService.getPageDeclareEvaluationInfoQuery(page, user.getBaseOrgId(), declareEvaluationInfo.getDeptId(),declareEvaluationInfo.getUserName(),declareEvaluationInfo.getErpNo(),declareEvaluationInfo.getYear(),declareEvaluationInfo.getAuditStatus());
            if (!page.getResult().isEmpty()) {
                page.getResult().forEach(p -> {
                    if (null != p.getYear() && p.getYear() == 0) {
                        p.setYear(null);
                    }
                });
            }
            rs.setData(page);
            LOG.info("查询成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查询失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 人才评价申报查询导出报表
     */
    @GetMapping("/exportExcelOfDeclareEvaluationQuery")
    public void exportExcelOfDeclareEvaluationInfoQuery(HttpServletRequest request, HttpServletResponse response, TDeclareEvaluationInfo declareEvaluationInfo) {
        try {
            User user = this.getLoginUserInfo(request);
            //查询要导出的报表数据
            List<TDeclareEvaluationInfo> list = declareEvaluationService.getDeclareEvaluationInfoQuery(user.getBaseOrgId(), declareEvaluationInfo.getDeptId(),declareEvaluationInfo.getUserName(),declareEvaluationInfo.getErpNo(),declareEvaluationInfo.getYear(),declareEvaluationInfo.getAuditStatus());
            String personYearPointDetailHead = "ERP编号,姓名,申报年份,部门,申报状态";
            //获取标题
            String[] personYearPointDetailHeads = personYearPointDetailHead.split(",");
            //sheet名称
            String sheetName = "人才评价申报查询导出";
            //获取数据
            String[][] Content = ExcelExportUtil.declareEvaluationInfoQuerHead(list, personYearPointDetailHeads);
            //创建workbook
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook = ExcelExportUtil.exportExcel(workbook, personYearPointDetailHeads, Content, sheetName);//基本信息
            //响应生成excel
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            LOG.info("开始导出excel报表");
            ExcelExportUtil.exportExcel2Browser(response, sheetName + sdf.format(new Date()), workbook);
            LOG.info("导出excel报表成功");
        } catch (Exception e) {
            LOG.error("导出失败");
            e.printStackTrace();
        }
    }
}
